package com.tanhua.mongo.api;

import com.tanhua.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.pojo.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询缘分值最高的推荐用户
    @Override
    public RecommendUser todayBest(Long userId) {
        //1 创建查询条件
        Query query = Query.query(Criteria.where("toUserId").is(userId));

        //2 设置评分倒序排序，只返回一条数据
        query.limit(1).with(Sort.by(Sort.Order.desc("score")));

        //3  查询数据
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //4 判断查询结果是否为空，如果为空就要制定一个默认用户
        if (recommendUser == null) {
            //选择缘分值最高的用户作为推荐用户
            query = new Query().limit(1).with(Sort.by(Sort.Order.desc("score")));
            recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        }

        //返回结果
        return recommendUser;
    }

    //根据toUserId(给谁推荐)查询所有的推荐好友
    @Override
    public List<RecommendUser> findByUserId(Long toUserId) {
        //封装查询条件
        Query query = Query.query(Criteria.where("toUserId").is(toUserId));
        //执行查询
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

    //根据  推荐用户id  和 当前用户Id   查询推荐好友数据
    @Override
    public RecommendUser find(Long userId, Long toUserId) {
        //1 封装查询条件
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));

        //2 执行查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //3 查询的结果如果为空，手动指定缘分值评分
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(90d);
        }

        //4 返回结果
        return recommendUser;
    }

    //查询推荐好友   随机获取10个推荐好友
    @Override
    public List<RecommendUser> queryCardsList(Long userId) {
        //1. 查询已经喜欢/不喜欢的用户
        List<UserLike> userLikeList = mongoTemplate.find(Query.query(
                Criteria.where("userId").is(userId)
        ), UserLike.class);

        //获取喜欢/不喜欢的用户ids
        List<Long> ids = userLikeList.stream()
                .map(UserLike::getLikeUserId).collect(Collectors.toList());

        //2. 创建查询条件，需要排除已经喜欢/不喜欢的用户
        Criteria criteria = Criteria.where("toUserId").is(userId)  //推荐给指定的用户  推荐给谁
                .and("userId").nin(ids);//排除已经喜欢/不喜欢

        //3. 使用MongoDB的统计API，实现随机查询10个推荐用户
        //创建统计内容
        TypedAggregation<?> aggregation = Aggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria),//设置查询的条件
                Aggregation.sample(10) //设置样本数量需要放在最后
        );
        //执行统计分析
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);

        //返回结果
        return results.getMappedResults();
    }
}
